#include "Avalon/ServiceLocatorTests/ServiceLocatorTests.hpp"
#pragma warning(disable : 4003)
#include <libjson.h>
#pragma warning(default : 4003)
#include "Avalon/Base/Handle.hpp"
#include "Avalon/IOTests/MockClientChannel.hpp"
#include "Avalon/ServiceLocator/ServiceChannelFactory.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::IO::Tests;
using namespace Avalon::ServiceLocator;
using namespace Avalon::ServiceLocator::Tests;
using namespace libjson;
using namespace std;

ServiceChannelFactory*
    ServiceLocator::Tests::CreateTestServiceChannelFactory() {
  map<string, ServiceChannelFactory::ChannelBuilder> testChannelBuilders =
    ServiceChannelFactory::GetDefaultChannelBuilders();
  testChannelBuilders.insert(make_pair("test", TestChannelBuilder));
  ServiceChannelFactory* serviceChannelFactory = new ServiceChannelFactory(
    ServiceChannelFactory::GetDefaultEncoderBuilders(),
    ServiceChannelFactory::GetDefaultDecoderBuilders(),
    ServiceChannelFactory::GetDefaultConnectionBuilders(),
    ServiceChannelFactory::GetDefaultReaderBuilders(),
    ServiceChannelFactory::GetDefaultWriterBuilders(),
    testChannelBuilders,
    ServiceChannelFactory::GetDefaultDataShuttleFactoryBuilders());
  return serviceChannelFactory;
}

Carry<Channel> ServiceLocator::Tests::TestChannelBuilder(
    ServiceChannelBuilder builder, const string& node) {
  JSONNode jsonNode = parse(node);
  intptr_t serverAddress = static_cast<intptr_t>(jsonNode["address"].as_int());
  MockServerConnection* serverConnection =
    reinterpret_cast<MockServerConnection*>(serverAddress);
  MockClientChannel* channel = new MockClientChannel(serverConnection);
  return channel;
}
